<template>
  <el-main height="">
    <el-form size="normal">
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="addBtn">新增菜单</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格在此 -->
    <el-table
      :height="tableHeight"
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="menuId"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="title" label="菜单名称"></el-table-column>
      <el-table-column prop="type" label="菜单类型" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type== '0' ">目录</el-tag>
          <el-tag v-if="scope.row.type== '1' " type="success">菜单</el-tag>
          <el-tag v-if="scope.row.type== '2' " type="warning">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="路由名称"></el-table-column>
      <el-table-column prop="path" label="路由地址"></el-table-column>
      <el-table-column prop="url" label="组件路径"></el-table-column>
      <el-table-column prop="code" label="权限字段"></el-table-column>
      <el-table-column prop="orderNum" label="菜单序号"></el-table-column>
      <el-table-column label="操作" width="200px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" plain size="small" icon="el-icon-edit" @click="editBtn(scope.row)">编辑</el-button>
          <el-button type="danger" plain size="small" icon="el-icon-delete" @click="deleteBtn(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <sys-dialog :title="dialog.title"
                :visible="dialog.visible"
                :width="dialog.width"
                :height="dialog.height"
                @onClose="onClose"
                @onConfirm="onConfirm">
      <div slot="content">
        <el-form :model="addModel" ref="addRef" :rules="rules" label-width="80px" size="small">
          <el-row>
            <el-col :span="24" :offset="0">
              <el-form-item prop="type" label="菜单类型">
                <el-radio-group v-model="addModel.type">
                  <el-radio label="0">目录</el-radio>
                  <el-radio label="1">菜单</el-radio>
                  <el-radio label="2">按钮</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="parentName" label="上级菜单">
                <el-input @click.native="selectParent" v-model="addModel.parentName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="title" label="菜单名称">
                <el-input v-model="addModel.title"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col v-if="addModel.type!=2 " :span="12" :offset="0">
              <el-form-item label="菜单图标">
                <el-input v-model="addModel.icon"></el-input>
              </el-form-item>
            </el-col>
            <el-col v-if="addModel.type==1" :span="12" :offset="0">
              <el-form-item prop="name" label="路由名称">
                <el-input v-model="addModel.name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row v-if="addModel.type== 1 ">
            <el-col :span="12" :offset="0">
              <el-form-item prop="path" label="路由地址">
                <el-input v-model="addModel.path"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="url" label="组件路径">
                <el-input v-model="addModel.url"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item label="权限字段">
                <el-input v-model="addModel.code"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item label="菜单序号">
                <el-input v-model="addModel.orderNum"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </sys-dialog>
    <!--    上级菜单弹框-->
    <sys-dialog :title="parentDialog.title" :width="parentDialog.width"
                :height="parentDialog.height" :visible="parentDialog.visible"
                @onClose="onParentClose" @onConfirm="onParentConfirm">
      <div slot="content">
        <el-tree ref="parentRef" :data="parentData" :props="parentDefaultProps"
                 @node-click="parentHandleNodeClick"
                 node-key="menuId"
                 empty-text="暂无数据"
                 :show-checkbox="false"
                 :highlight-current="true"
                 default-expand-all
                 :expand-on-click-node="false"
        >
          <div slot-scope="{node,data}">
            <!--如果没有下级，显示文档图标-->
            <span v-if="data.children.length == 0">
              <i style="margin-left: 3px" class="el-icon-document"></i>
            </span>
            <!--有下级，判断是否展开 -->
            <span v-else @click.stop="openBtn(data)">
              <i v-if="data.open" style=" margin-left: 3px" class="el-icon-plus"></i>
              <i v-else style="margin-left: 3px" class="el-icon-minus"></i>
            </span>
            <span style="margin-left: 3px">{{ node.label }}</span>
          </div>
        </el-tree>
      </div>
    </sys-dialog>
  </el-main>

</template>

<script>
//这是弹窗组件
import SysDialog from '@/components/dialog/StstemDialog.vue'

import {getListApi, getParentListApi, addMenuApi, editMenuApi, deleteMenuApi} from '@/api/menu'

export default {
  //注册弹框组件
  components: {
    SysDialog,
  },
  data() {
    return {
      tableHeight: this.tableHeight = window.innerHeight - 220,
      tableData: [],
      //弹框属性
      dialog: {
        title: '',
        visible: false,
        width: 800,
        height: 280,
      },
      //表单绑定属性
      addModel: {
        editType: '', //区分新增或者编辑 0 新增 1 编辑
        menuId: '',
        parentId: '',
        title: '',
        code: '',
        name: '',
        path: '',
        icon: '',
        parentName: '',
        orderNum: '',
        type: '',
        url: ''
      },
      //表单验证规则
      rules: {
        parentName: [{trigger: 'blur', required: true, message: '请选择上级菜单'}],
        title: [{trigger: 'blur', required: true, message: '请填写菜单名称'}],
        code: [{trigger: 'blur', required: true, message: '请填写权限字段'}],
        name: [{trigger: 'blur', required: true, message: '请填路由名称'}],
        path: [{trigger: 'blur', required: true, message: '请填写路由地址'}],
        type: [{trigger: 'blur', required: true, message: '请选择菜单类型'}],
      },
      //上级菜单弹框属性
      parentDialog: {
        title: '选择上级菜单',
        width: 400,
        height: 400,
        visible: false
      },
      //上级菜单树
      parentData: [],
      parentDefaultProps: {
        children: 'children',
        label: 'title'
      },
      selectNode: {
        menuId: '',
        title: ''
      },
      openBtn() {
        //上级部门节点加号和减号点击事件
        data.open = !data.open;
        this.$refs.parentTree.store.nodesMap[data.menuId].expanded = !data.open;
      }
    }
  },

  created() {
    this.getList()
  },
  methods: {
    //上级菜单节点点击事件
    parentHandleNodeClick(n) {
      this.selectNode.menuId = n.menuId
      this.selectNode.title = n.title
      console.log(n)
    },
    //选择上级菜单
    async selectParent() {
      let res = await getParentListApi()
      if (res && res.code == 200) {
        this.parentData = res.data
      }
      this.parentDialog.visible = true
    },
    //上级菜单弹框关闭
    onParentClose() {
      this.parentDialog.visible = false
    },
    //上级菜单弹框确定
    onParentConfirm() {
      this.addModel.parentId = this.selectNode.menuId;
      this.addModel.parentName = this.selectNode.title
      this.parentDialog.visible = false
    },
    async getList() {
      let res = await getListApi();
      if (res && res.code == 200) {
        console.log(res)
        this.tableData = res.data
      }
    },
    //新增按钮
    addBtn() {
      this.dialog.title = "新增菜单"
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      this.addModel.editType = '0'
    },
    editBtn(row) {
      this.dialog.title = "编辑用户"
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      //拷贝数据
      this.$objCopy(row, this.addModel)
// 确保菜单类型被正确更新
      this.$nextTick(() => {
        this.addModel.type = row.type; // 将菜单类型更新为当前行的类型
      });
      this.dialog.editType = '1'

    },
    async deleteBtn(row) {
      let confirm = await this.$myconfirm("确定删除该数据吗？")
      if (confirm) {
        let result = await deleteMenuApi({menuId: row.menuId})
        if (result && result.code === 200) {
          //信息提示
          this.$message({type: 'success', message: result.msg})
          //刷新表格
          this.getList()
          this.dialog.visible = false;
        }
      }
    },
    //弹框取消
    onClose() {
      this.dialog.visible = false;
    },
    //弹框确定
    onConfirm() {
      this.$refs.addRef.validate(async (valid) => {
        if (valid) {
          let result = null
          if (this.addModel.editType == '0') {
            result = await addMenuApi(this.addModel)
          } else {
            result = await editMenuApi(this.addModel)
          }
          if (result && result.code == 200) {
            //信息提示
            this.$message({type: 'success', message: result.msg})
            //刷新表格
            this.getList()
            this.dialog.visible = false;
          }
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>

</style>
